﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;

public partial class InStoreDataMeasures_MeasuresGroup_Item : PageBase
{
    private const string LAST_PAGE = "MeasuresGroup_List.aspx";
    private const string REQUST_MGroupID = "MGID";
    private int MGID = 0;
    private OrgainzationManager orgManager = new OrgainzationManager();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUST_MGroupID] != null)
        {
            int.TryParse(Request.QueryString[REQUST_MGroupID],out MGID);
        }
       
       
        if (!IsPostBack)
        {
            BindGrid();
            InitForm();
            
        }
    }

    private void InitForm()
    {
        if (MGID != 0)
        {
           
        }
        else
        {
            btnSave.Enabled = false;
            btnAddItem.Enabled = false;
        }
         
           
       
    }

    private void BindGrid()
    {
        if (MGID != 0)
        {
            string sb = "";
            if (ddlValid.SelectedValue != "")
            {
                sb += " AND ACTIVE='"+ddlValid.SelectedValue+"' ";
            }
            if (txt_key.Text != "")
            {
                sb += " AND (CODE LIKE '%"+txt_key.Text+"%' OR NAME LIKE '%"+txt_key.Text+"%') ";
            }

            MeasuresGroupItemManager mgim = new MeasuresGroupItemManager();
            gvITEM.DataSource = mgim.GetMeasuresGroupItemTable(MyStateManager.DomainID, MGID.ToString(),sb);
            gvITEM.DataBind();
        }
        else
        {
            ShowMessage("非法访问或参数错误！");
            return;
        }
     
    }

    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvITEM_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();

    }

    protected void gvITEM_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Grid_RowDataBound(sender, e);
            MEASURE_ITEMS measureitem = (MEASURE_ITEMS)e.Row.DataItem;
            if (measureitem != null)
            {
                MeasuresGroupItemDetailManager mgidm = new MeasuresGroupItemDetailManager();
             
                GridViewRow row = e.Row;
                GridView gv = new GridView();
                gv = (GridView)row.FindControl("gvDETAIL");

                DataView dv = new DataView();
                DataSet ds = mgidm.GetMeasureGroupItemDetailTable(MyStateManager.DomainID, measureitem.ID.ToString());
                dv = ds.Tables[0].DefaultView;
                gv.DataSource = dv;
                gv.DataBind();


            }
        }

    }
    protected void ddlMobileUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Response.Redirect(String.Format(RESPONSE_NEW_PAGE, ddlMobileUser.SelectedValue,Request.Url));
    }


    protected void btnExport_Click(object sender, EventArgs e)
    {
        //this.Response.Redirect("..\\ExportExcel\\ExportExcel.aspx?Table=UserRouteList&User=" + this.ddlMobileUser.SelectedValue + "&VALID=1");
    }

  
    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        this.gvITEM.DataSource = null;
        this.gvITEM.DataBind();
        //OrgHelper.BindUsersDDLEX(MyStateManager.DomainID, txtHidOrg.Value, ddlMobileUser);
        //ddMobileUser
        this.InitForm();
    }

    /// <summary>
    /// 绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;

        if (DetailsView1.DataItem == null)
        {
            chk.Checked = true;
        }
        else
        {
            MEASURE_ITEMS mi=DetailsView1.DataItem as MEASURE_ITEMS;
            DropDownList ddl_state = DetailsView1.FindControl("ddl_state") as DropDownList;
            TextBox txtCode = DetailsView1.FindControl("txtCode") as TextBox;
            txtCode.Enabled = false;
            ddl_state.SelectedValue = mi.SHOWSITE.ToString();
        }
    }

    protected void btnSaveItem_Click(object sender, EventArgs e)
    {
        try
        {
            MEASURE_ITEMS MGROUPITEM = null;
            MeasuresGroupItemManager manager = new MeasuresGroupItemManager();

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                MGROUPITEM = new MEASURE_ITEMS();
                MGROUPITEM.CREATETIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);

            }
            else
            {
                MGROUPITEM = manager.Load(int.Parse(gvITEM.SelectedDataKey.Value.ToString()));
            }
            TextBox txtCode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox txtName = DetailsView1.FindControl("txtName") as TextBox;
            TextBox txtSEQUENCE = DetailsView1.FindControl("txtSEQUENCE") as TextBox;
            DropDownList ddl_state = DetailsView1.FindControl("ddl_state") as DropDownList;
            MGROUPITEM.CODE = txtCode.Text;
            MGROUPITEM.NAME = txtName.Text;
            if (txtSEQUENCE.Text != "")
            {
                MGROUPITEM.SEQUENCE = decimal.Parse(txtSEQUENCE.Text);
            }
            MGROUPITEM.SHOWSITE = decimal.Parse(ddl_state.SelectedValue);
            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            MGROUPITEM.ACTIVE = cbxValid.Checked;
            MGROUPITEM.DOMAINID = MyStateManager.DomainID;
            MGROUPITEM.LAST_UPDATER = MyStateManager.UserID.ToString();
            MGROUPITEM.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            MGROUPITEM.MEASUERGROUPID = MGID;
            string LogContent = "";

            if (MGROUPITEM.State == DataRowState.Added)
            {
                LogContent += "添加指标项：" + txtName.Text;
            }
            else
            {
                LogContent += MGROUPITEM.CODE + "信息发生变动，变动后信息是：";
                LogContent += "指标项名称：" + MGROUPITEM.NAME;
                LogContent += "有效：" + MGROUPITEM.ACTIVE;
                LogContent += "操作人：" + MGROUPITEM.LAST_UPDATER;
            }
            if (MGROUPITEM.State == DataRowState.Added)
            {
                if (manager.Load(MGROUPITEM.DOMAINID, MGROUPITEM.CODE) != null)
                {
                    ShowMessage("代码已存在."); return;
                }
            }
            manager.Save(MGROUPITEM);

            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.AUTOSYNC,
            MyStateManager.Name, MyStateManager.UserID, LogContent);

                   
            ShowMessage("保存成功", Request.Url.ToString());



        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "常规指标类新增 - 异常:" + ex.Message);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

    }
    protected void btnAddItem_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
}
